package primetest.impl.controler;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.security.RolesAllowed;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import primetest.dto.PatientDto;
import primetest.faces.PatientListModel;
import primetest.impl.manager.SessionManager;
import primetest.impl.service.PatientService;

@Named("patientListControler")
@SessionScoped
@RolesAllowed({ "patient", "admin" })
public class PatientListControler implements Serializable {

	/**
	 *
	 */
	private static final long serialVersionUID = -118678570623180842L;

	private Logger logger = LoggerFactory.getLogger(this.getClass());


	@Inject
	private SessionManager sessionManager;


	private PatientListModel patientList = null;
	private List<PatientDto> basePatientList;
	private PatientDto selectedPatient = new PatientDto();
	private PatientDto backPatient = new PatientDto();
	private PatientDto newPatient = new PatientDto();
	private int first;
	private boolean editMode = false;
	private boolean addMode = false;
	private List<PatientDto> filteredTerms;

	public boolean getEditMode() {
		return editMode;
	}

	public boolean getAddMode() {
		return addMode;
	}

	@Inject
	private PatientService patientService;


	@RolesAllowed({ "patient", "admin" })
	public PatientListModel getPatientList() {
		if (basePatientList == null) {
			try {
				basePatientList = patientService.getPatientList();
				patientList = new PatientListModel(basePatientList);
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}

		return basePatientList == null ? new PatientListModel(
				new ArrayList<PatientDto>()) : patientList;
	}

	public void deletePatient() throws IOException {
		patientService.deletePatient(selectedPatient.getSeq());
	}


	public PatientDto getSelectedPatient() {
		return selectedPatient;
	}

	public void setSelectedPatient(PatientDto selectedPatient) {
		this.selectedPatient = selectedPatient;
	}

	public int getFirst() {
		return first;
	}

	public void setFirst(int first) {
		this.first = first;
	}

	public PatientDto getNewPatient() {
		return newPatient;
	}

	public void setNewPatient(PatientDto newPatient) {
		this.newPatient = newPatient;
	}

	public void saveEditPatient() {
		try {
			if (selectedPatient.isValid()) {
				selectedPatient.saveDate();
				patientService.updatePatient(selectedPatient);
				logger.debug("save the selectedPatient" + " gender: "
						+ selectedPatient.getGender() + " seq: "
						+ selectedPatient.getSeq());
				editMode = false;
			} else {
				sessionManager.addGlobalMessageFatal("Birthday invalid", null);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void startEditPatient() {
		editMode = true;
		backPatient.setAll(selectedPatient);
	}

	public void cancelEditPatient() {
		editMode = false;
		selectedPatient.setAll(backPatient);
	}

	public void startAddPatient() {
		addMode = true;
		newPatient = new PatientDto();
	}

	public void addNewPatient() throws IOException {
		if (newPatient.isValid()) {
			newPatient.saveDate();
			patientService.addPatient(newPatient);
			addMode = false;
		} else {
			sessionManager.addGlobalMessageFatal("Build Date invalid", null);
		}
	}

	public void cancelAddPatient() {
		addMode = false;
		newPatient = new PatientDto();
	}

	public List<PatientDto> getFilteredTerms() {
		return filteredTerms;
	}

	public void setFilteredTerms(List<PatientDto> filteredTerms) {
		this.filteredTerms = filteredTerms;
	}
}
